package cn.wp.ewallet.wallet.dao.impl;

import cn.wp.common.core.dao.JdbcSupportTemplate;
import cn.wp.common.core.dao.SqlString;
import cn.wp.ewallet.wallet.dao.StatInfoDao;
import cn.wp.ewallet.wallet.model.entity.ServiceChargeStatDoo;
import cn.wp.ewallet.wallet.model.entity.StatInfoDo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by yangjin on 2020/2/5
 */
@Repository
public class StatInfoDaoImpl implements StatInfoDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public StatInfoDo selectstatInfoByStatType(String statType, String date) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select t.* from stat_info t where t.STAT_TYPE = ? and t.STAT_DATE = ?;");
        return jdbcSupportTemplate.selectObject(sqlString.build(),new Object[]{statType, date}, StatInfoDo.class);
    }

    @Override
    public List<ServiceChargeStatDoo> selectServiceChargeStatInfo(String year, String month) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("SELECT DISTINCT t1.STAT_DATE AS DATE, t2.YES_CURRENCY AS trans_account_amount, t3.YES_CURRENCY AS usdt_mention_amount, t4.YES_CURRENCY AS shqc_mention_amount");
        sqlString.add("FROM stat_info t1, stat_info t2, stat_info t3, stat_info t4");
        sqlString.add("WHERE t2.STAT_DATE = t1.STAT_DATE");
        sqlString.add("    AND t3.STAT_DATE = t1.STAT_DATE");
        sqlString.add("    AND t4.STAT_DATE = t1.STAT_DATE");
        sqlString.add("    AND t2.STAT_TYPE = '1000'");
        sqlString.add("    AND t3.STAT_TYPE = '1100'");
        sqlString.add("    AND t4.STAT_TYPE = '1200'");
        sqlString.add("    AND t1.STAT_DATE IN (");
        sqlString.add("        SELECT DISTINCT t1.STAT_DATE");
        sqlString.add("        FROM stat_info t1");
        sqlString.add("        WHERE YEAR(t1.STAT_DATE) = ?");
        sqlString.add("        AND MONTH(t1.STAT_DATE) = ?");
        sqlString.add("    )");
        sqlString.add("ORDER BY DATE ASC;");
        return jdbcSupportTemplate.selectList(sqlString.build(), new Object[]{year, month}, ServiceChargeStatDoo.class);
    }

    @Override
    public void insertStatInfo(StatInfoDo statInfoDo) {
        jdbcSupportTemplate.insert(statInfoDo);
    }
}
